﻿using RayD.Core;
using System;
using System.Collections.Generic;
using System.Text;

namespace RayD.App.Setup
{
    /// <summary>
    /// 即时通讯客户端配置
    /// </summary>
    public class IMSetup
    {
        public static void InitClient()
        {
            if (AppSettings.ImClientOption.Enabled)
            {
                ImHelper.Initialization(new ImClientOptions
                {
                    Redis = new CSRedis.CSRedisClient(AppSettings.ImClientOption.RedisClient),
                    Servers = AppSettings.ImClientOption.Servers.Split(";")
                });

                //ImHelper.Instance.OnSend += (s, e) =>
                //    Console.WriteLine($"ImClient.SendMessage(server={e.Server},data={JsonConvert.SerializeObject(e.Message)})");

                ImHelper.EventBus(
                    t =>
                    {
                        var onlineUids = ImHelper.GetClientListByOnline();
                        ImHelper.SendMessage(t.clientId, onlineUids, new { type = "online", content = new { id = t.clientId, online = true } }.ToJson());
                    },
                    t =>
                    {
                        var onlineUids = ImHelper.GetClientListByOnline();
                        ImHelper.SendMessage(t.clientId, onlineUids, new { type = "online", content = new { id = t.clientId, online = false } }.ToJson());
                    });
            }
        }
    }
}
